package com.whit.nuoge.dao.impl;


import com.whit.nuoge.dao.UserDao;
import com.whit.nuoge.pojo.User;

import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public int save(User user) {
        String sql = "insert into t_user(`username`,`password`,`phone`,`weixinnickname`,`weixinopenid`,`status`,`memo`,`picurl`,`weixinpicurl`,`email`,`groupid`,`birthday`) values(?,?,?,?,?,?,?,?,?,?,?,?)";
        return update(sql, user.getUsername(), user.getPassword(), user.getPhone(),user.getWeixinnickname(),user.getWeixinopenid(),user.getStatus(),
                user.getMemo(),user.getPicurl(),user.getWeixinpicurl(),user.getEmail(),user.getGroupid(),user.getBirthday());
    }

    @Override
    public int update(User user) {
        String sql = "update t_user set `username`=?,`password`=?,`phone`=? ,`weixinnickname`=?,`weixinopenid`=?,`status`=?,`memo`=?,`picurl`=?,`weixinpicurl`=?,`email`=?,`groupid`=?,`birthday`=?  where id=?";
        return update(sql,user.getUsername(), user.getPassword(), user.getPhone(),user.getWeixinnickname(),user.getWeixinopenid(),user.getStatus(),user.getMemo(),user.getPicurl(),user.getWeixinpicurl(),user.getEmail(),user.getGroupid(),user.getBirthday(),user.getId());
    }

    @Override
    public int deleteById(Integer id) {
        String sql = "delete from t_user where id = ?";
        return update(sql,id);
    }


    @Override
    public User queryById(Integer id) {
        String sql = "select * from t_user where id = ?";
        return queryOne(User.class,sql,id);
    }


    @Override
    public Integer queryForPageCountByQueryString(String queryString) {
        String sql = "";
        Object count = null;
        if(queryString == null || queryString == ""){
            sql = "select count(*) from t_user ";
            count = queryForSingleValue(sql);
        }else{
            queryString = "%" + queryString + "%";
            sql = "select count(*) from t_user where username like ? or memo like ? or phone like ? ";
            count = queryForSingleValue(sql,queryString,queryString,queryString);
        }
        return new Integer(count.toString());
    }

    @Override
    public List<User> queryForPageItemsByQueryString(Integer begin, Integer pageSize, String queryString) {
        String sql = null;
        if(queryString == null || queryString == ""){
            sql =  "select `username`,`password`,`phone`,`weixinnickname`,`weixinopenid`,`status`,`memo`,`picurl`,`weixinpicurl`,`email`,`groupid`,`groupid`,`birthday`,`id` " +
                    "from t_user  limit ? , ?";
            return queryList(User.class,sql,begin,pageSize);

        }else{
            queryString = "%" + queryString + "%";
            sql =  "select `username`,`password`,`phone`,`weixinnickname`,`weixinopenid`,`status`,`memo`,`picurl`,`weixinpicurl`,`email`,`groupid`,`groupid`,`birthday`,`id` " +
                    "from t_user where username like ? or memo like ? or phone like ? limit ? , ?";
            return queryList(User.class,sql,queryString,queryString,queryString,begin,pageSize);
        }
    }
}
